package com.example.a2107gitee

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity(),View.OnClickListener{

    private val btn:Button by lazy { findViewById(R.id.startService) }
    private lateinit var stop:Button
    private  lateinit var intent:Intent

    //2
//    private var stop1:Button? =null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn.setOnClickListener(this)
        stop = findViewById(R.id.stogitpService)
        stop.setOnClickListener(this)
        //2
//       stop1?.setOnClickListener(this)
    }

    //启动服务
    private fun startService(){
        intent = Intent(this,MusicService::class.java)
        startService(intent)
    }

    private fun stopServcice(){
        stopService(intent)
    }

    override fun onClick(v: View?) {
        if (v != null) {
            when(v.id){
                R.id.startService -> {
                    startService()
                }
                R.id.stogitpService -> {
                    stopServcice()
                }


            }
        }

    }
}